<template>
	<BaseContainer>
		<aplid-base-crud
			v-if="showType == '全部'"
			ref="crud"
			class="w-100"
			v-bind="baseConfig"
			:operateFormate="handleOperate"
			:paramsObj="{
        type:0,
        ...paramsObj
      }"
			:onLoadFormate="onLoadFormate"
      :searchConfig="searchConfig(RegionId,userInfo.authArea)"
		>
			<template v-slot:pageTitle="{ total }" v-if="RegionName">
				<span class="text-large">{{ RegionName }}内机构（{{ total }}家）</span>
			</template>
			<template v-slot:menuRight>
				<el-radio-group v-model="showType" size="default">
					<el-radio-button label="全部" />
					<el-radio-button label="街道" />
				</el-radio-group>
			</template>
		</aplid-base-crud>

		<el-page-header v-else @back="goBack">
			<template #content>
				<span class="text-large font-600 mr-3" v-if="RegionName">{{ RegionName }}内机构（{{ organizationNumName }}家）</span>
			</template>
			<div class="mt-4 text-sm font-bold">
				<integrity :RegionId="RegionIdName"></integrity>
			</div>
		</el-page-header>

		<aplid-dialog
			@close="onClose"
			width="85%"
			v-model="visibleForm"
			append-to-body
			whickComm="Archives"
			:customeHeader="customeHeader"
			:model="{ ...nowRowData, ...detailHeadData }"
		>
			<transition name="fade" mode="out-in">
				<div class="dialog-content w-100" v-show="!Loading" v-if="visibleForm">
					<orgArchives :nowRowData="nowRowData" :showBtn="false"/>
				</div>
			</transition>
			<PageLoading v-if="Loading" />
		</aplid-dialog>
	</BaseContainer>
</template>

<script setup lang="ts">
import apiMod from '@/apiMod/index'
import { baseConfig,searchConfig } from './config'
import orgArchives from '../../basicData/serviceOrg/components/Archives/index.vue'
import integrity from './integrity/index.vue'
import { useCommonStore } from '@/stores/common'
import { getStore } from '@/utils/store'

const props = defineProps<{
	RegionId?: string | number
	Region?: string
	organizationNum?: string | number
}>()

const paramsObj = ref({
	areaId: props.RegionId,
})
const userInfo = getStore('userInfo') || {}

let areaObj = {
	provinceId: undefined,
	cityId: undefined,
	areaId: undefined,
	streetId: undefined,
	communityId: undefined,
}
if (userInfo.authProvince) areaObj.provinceId = userInfo.authProvince
if (userInfo.authCity) areaObj.cityId = userInfo.authCity
if (userInfo.authArea) areaObj.areaId = userInfo.authArea
if (userInfo.authStreet) areaObj.streetId = userInfo.authStreet
if (userInfo.authCommunity) areaObj.communityId = userInfo.authCommunity
if (props.RegionId) {
	paramsObj.value = {
		areaId: props.RegionId,
	}
} else {
	paramsObj.value = {
		...areaObj,
	}
}

const RegionName = ref(props.Region)
const RegionIdName = ref(props.RegionId)
const organizationNumName = ref(props.organizationNum)

const { setTempParams, setGloablParams, setMoreSearch } = useCommonStore()
const { Loading, detailHeadData } = storeToRefs(useCommonStore())

const visibleForm = ref(false)
const customeHeader = ref({
	type: 'CardOne',
	title: 'organizationName',
	avatar: 'organizationLogo',
	tipList: [{ prop: 'operateStatusLabel', type: 'tip-success', label: '运营状态' }],
	tagList: [
		{ prop: 'organizationTypeLabel', custome: 'tag', type: 'success', label: '机构类型' },
		{
			prop: 'integrity',
			type: 'success',
			label: '材料完整度',
			style: {
				background: '#1e80ff',
				padding: '2px 4px',
				borderRadius: '5px',
				fontWeight: 600,
			},
		},
	],
	menuBtn: [],
})

const nowRowData = ref({})
const onLoadFormate = async (arr, paramsQuery) => {
	organizationNumName.value = paramsQuery.total
	return Promise.resolve(arr)
}
const onClose = () => {
	setTempParams({})
	setGloablParams({})
	setMoreSearch({})
}

const getServiceOrgInfo = (creditCode) => {
	return new Promise((resolve, reject) => {
		if (!creditCode) {
			reject()
		}
		apiMod.view_dpServiceOrgBase_creditCode(creditCode).then((res) => {
			resolve(res.data)
		})
	})
}

const handleOperate = (row, type) => {
	return new Promise(async (resolve, reject) => {
		if (type == 'refresh') {
			resolve(row)
			return
		}
		nowRowData.value = await getServiceOrgInfo(row.creditCode)
		visibleForm.value = true
	})
}

const showType = ref('全部')

const goBack = () => {
	showType.value = '全部'
}

onMounted(() => {
	if (!userInfo.authArea) return
	apiMod.admin_sysadministration_detail(userInfo.authArea).then((res) => {
		RegionName.value = res.data.administrationName
		RegionIdName.value = res.data.administrationId
	})
})
</script>

<style lang="scss" scoped>
.text-large {
	font-family: 'Microsoft YaHei Bold';
	font-weight: 700;
	font-size: 16px;
	text-align: left;
	color: #0052d9;
}
</style>
